package com.itheima.web.servlet;

import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {
    private UserService us = new UserServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //获取表单中的method值
        String methodName = request.getParameter("method");
        if ("findAll".equals(methodName)) {
            findAll(request, response);
        } else if ("add".equals(methodName)) {
            add(request, response);
        } else if ("findById".equals(methodName)) {
            findById(request, response);
        } else if ("update".equals(methodName)) {
            update(request, response);
        } else if ("delete".equals(methodName)) {
            delete(request, response);
        }
    }

    //删除用户
    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        //UserService us = new UserServiceImpl();
        us.deleteUser(id);
        response.sendRedirect(request.getContextPath() + "/userServlet?method=findAll");
    }

    //修改用户
    private void update(HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, String[]> map = request.getParameterMap();
            User user = new User();
            BeanUtils.populate(user, map);
            //UserService us = new UserServiceImpl();
            us.updateUser(user);
            response.sendRedirect(request.getContextPath() + "/userServlet?method=findAll");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //回显
    private void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        //UserService us = new UserServiceImpl();
        User user = us.findById(id);
        request.setAttribute("user", user);
        request.getRequestDispatcher("/update.jsp").forward(request, response);
    }

    //添加用户
    private void add(HttpServletRequest request, HttpServletResponse response) {
        try {

            Map<String, String[]> map = request.getParameterMap();
            User user = new User();
            BeanUtils.populate(user, map);
            //UserService us = new UserServiceImpl();
            us.addUser(user);
            response.sendRedirect(request.getContextPath() + "/userServlet?method=findAll");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //查询用户
    private void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //UserService us = new UserServiceImpl();
        List<User> users = us.findAll();
        request.setAttribute("users", users);
        request.getRequestDispatcher("/list.jsp").forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
